﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using PersonaLibrary.Data;

namespace PersonaLibrary.ViewModel.CollectionViewModel
{
	internal sealed class CollectionViewModel<TEntity> : BaseCollectionViewModel<TEntity>
		where TEntity : class, IDBEntity
	{
		#region Private members
		
		private ItemsSelectorViewModel _itemsSelector;
		private char _selectedChar;
		private AlphabetViewModel.AlphabetViewModel _alphabetViewModel;
		private string _additionalLoadParametrs;

		#endregion

		public CollectionViewModel(string additionalLoadParametrs)
			: this()
		{
			_additionalLoadParametrs = additionalLoadParametrs;
		}

		public CollectionViewModel()
		{
			_itemsSelector = new ItemsSelectorViewModel();
			_itemsSelector.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
			                                  	{
													if(e.PropertyName == "Filter")
			                                  			RisePropertyChanged("CollectionFilter");
			                                  	};
			AlphabetViewModel = new AlphabetViewModel.AlphabetViewModel(typeof(TEntity).Name);
			AlphabetViewModel.PropertyChanged += AlphabetViewModelPropertyChanged;

			_itemsSelector.SetCollection(ObservableCollection);
			
			CollectionLoad();
		}

		void AlphabetViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if(e.PropertyName != "Registry")
				CollectionLoad();
		}

		protected override void CollectionLoad()
		{
			_itemsSelector.ResetFilter();

			ObservableCollection.Clear();

			IEnumerable<TEntity> executeResult;

			if (_alphabetViewModel.IsEnable)
				executeResult =
					Context.CreateAndExecuteQuery<TEntity>(string.Format("{0}?$filter=startswith({1},'{2}')&{3}", typeof (TEntity).Name,
					                                            _alphabetViewModel.SelectedProperty, _alphabetViewModel.SelectedChar,
																_additionalLoadParametrs));
			else
				executeResult = Context.CreateAndExecuteQuery<TEntity>(string.Format("{0}?{1}", typeof(TEntity).Name, _additionalLoadParametrs));

			foreach (TEntity singleResult in executeResult)
				ObservableCollection.Add(singleResult);

			ItemsSelector.SetCollection(ObservableCollection);

			RisePropertyChanged("Collection");
			RisePropertyChanged("ItemsSelector");
			RisePropertyChanged("CurrentPack");
		}

		public ItemsSelectorViewModel ItemsSelector
		{
			get { return _itemsSelector; }
			set 
			{ 
				_itemsSelector = value; 
				RisePropertyChanged("ItemsSelector"); 
			}
		}

		public Predicate<object> CollectionFilter
		{
			get
			{
				return _itemsSelector.Filter;
			}
		}
		
		public AlphabetViewModel.AlphabetViewModel AlphabetViewModel
		{
			get { return _alphabetViewModel; }
			set
			{
				_alphabetViewModel = value;
				RisePropertyChanged("AlphabetViewModel");
			}
		}

		public char SelectedChar
		{
			get { return _selectedChar; }
			set
			{
				_selectedChar = value;
				RisePropertyChanged("SelectedChar");
			}
		}

		public override void Dispose()
		{
			_alphabetViewModel.Dispose();
			base.Dispose();
		}
	}
}
